local assets = {
    Asset("ANIM", "anim/mami_anim_gun_tf.zip"),
    Asset("ANIM", "anim/mami_anim_gun1.zip"),
    Asset("ANIM", "anim/mami_anim_gun2.zip"),
    Asset("ANIM", "anim/mami.zip"),
}

local ex_fns = require("prefabs/player_common_extensions")

local brain = require("brains/magicalgirl_mamibrain")

SetSharedLootTable("magicalgirl_mami",
{
    {"yellowgem",  1.0},
    {"horrorfuel",  1.0},
    {"horrorfuel",  1.0},
    {"horrorfuel",  1.0},
    {"voidcloth",  1.0},
    {"voidcloth",  1.0},
    {"voidcloth",  1.0},
    {"voidcloth",  1.0},
    {"voidcloth",  1.0},
    {"voidcloth",  1.0},
})

local TARGET_DIST = 20
local MUST_TAGS = { "_combat" }
local CANT_TAGS = { "witch", "witchfriend" }
local function RetargetFn(inst)
    return FindEntity(inst, TARGET_DIST, function(guy)
        return inst.components.combat:CanTarget(guy)
    end, MUST_TAGS, CANT_TAGS)
end

local function KeepTargetFn(inst, target)
    return not IsEntityDead(inst) and inst.components.combat:CanTarget(target) and not target:HasOneOfTags(CANT_TAGS)
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddDynamicShadow()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddLight()
    inst.entity:AddLightWatcher()
    inst.entity:AddNetwork()

    inst.Transform:SetFourFaced()

    inst.AnimState:SetBank("wilson")
    inst.AnimState:SetBuild("mami")

    inst.AnimState:PlayAnimation("idle")

    inst.AnimState:Hide("ARM_carry")
    inst.AnimState:Hide("HAT")
    inst.AnimState:Hide("HAIR_HAT")
    inst.AnimState:Show("HAIR_NOHAT")
    inst.AnimState:Show("HAIR")
    inst.AnimState:Show("HEAD")
    inst.AnimState:Hide("HEAD_HAT")
    inst.AnimState:Hide("HEAD_HAT_NOHELM")
    inst.AnimState:Hide("HEAD_HAT_HELM")

    inst.AnimState:OverrideSymbol("fx_wipe", "wilson_fx", "fx_wipe")
    inst.AnimState:OverrideSymbol("fx_liquid", "wilson_fx", "fx_liquid")
    inst.AnimState:OverrideSymbol("shadow_hands", "shadow_hands", "shadow_hands")
    inst.AnimState:OverrideSymbol("snap_fx", "player_actions_fishing_ocean_new", "snap_fx")

    --Additional effects symbols for hit_darkness animation
    inst.AnimState:AddOverrideBuild("player_hit_darkness")
    inst.AnimState:AddOverrideBuild("player_receive_gift")
    inst.AnimState:AddOverrideBuild("player_actions_uniqueitem")
    inst.AnimState:AddOverrideBuild("player_wrap_bundle")
    inst.AnimState:AddOverrideBuild("player_lunge")
    inst.AnimState:AddOverrideBuild("player_attack_leap")
    inst.AnimState:AddOverrideBuild("player_superjump")
    inst.AnimState:AddOverrideBuild("player_multithrust")
    inst.AnimState:AddOverrideBuild("player_parryblock")
    inst.AnimState:AddOverrideBuild("player_emote_extra")
    inst.AnimState:AddOverrideBuild("player_boat_plank")
    inst.AnimState:AddOverrideBuild("player_boat_net")
    inst.AnimState:AddOverrideBuild("player_boat_sink")
    inst.AnimState:AddOverrideBuild("player_oar")

    inst.AnimState:AddOverrideBuild("player_actions_fishing_ocean_new")
    inst.AnimState:AddOverrideBuild("player_actions_farming")
    inst.AnimState:AddOverrideBuild("player_actions_cowbell")

    inst.AnimState:AddOverrideBuild("player_shadow_thrall_parasite")

    inst.DynamicShadow:SetSize(1.3, .6)

    --Default to electrocute light values
    inst.Light:SetIntensity(.8)
    inst.Light:SetRadius(.5)
    inst.Light:SetFalloff(.65)
    inst.Light:SetColour(255 / 255, 255 / 255, 236 / 255)
    inst.Light:Enable(false)

    inst.LightWatcher:SetLightThresh(.075)
    inst.LightWatcher:SetMinLightThresh(0.61) --for sanity.
    inst.LightWatcher:SetDarkThresh(.05)

    inst:AddTag("character")
    inst:AddTag("magicalgirl")
    inst:AddTag("witchfriend")
    inst:AddTag("walpurgisnight_friend")

    MakeCharacterPhysics(inst, 75, .5)

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")

    inst:AddComponent("inventory")

    inst:AddComponent("lootdropper")
    inst.components.lootdropper:SetChanceLootTable("magicalgirl_mami")

    inst:AddComponent("embarker")
    inst.components.embarker.embark_speed = TUNING.WILSON_RUN_SPEED

    inst:AddComponent("locomotor")
    ex_fns.ConfigurePlayerLocomotor(inst)

    inst:AddComponent("combat")
    inst.components.combat:SetDefaultDamage(TUNING.UNARMED_DAMAGE)
    inst.components.combat.hiteffectsymbol = "torso"
    inst.components.combat:SetAttackPeriod(5)
    inst.components.combat:SetRange(8)
    inst.components.combat:SetRetargetFunction(3, RetargetFn)
    inst.components.combat:SetKeepTargetFunction(KeepTargetFn)

    inst:AddComponent("health")
    inst.components.health:SetMaxHealth(TUNING.MAGICALGIRL_MAMI_HEALTH)
    inst.components.health.nofadeout = true

    MakeMediumBurnableCharacter(inst, "torso")
    inst.components.burnable:SetBurnTime(TUNING.PLAYER_BURN_TIME)
    inst.components.burnable.nocharring = true

    MakeLargeFreezableCharacter(inst, "torso")
    inst.components.freezable:SetResistance(4)
    inst.components.freezable:SetDefaultWearOffTime(TUNING.PLAYER_FREEZE_WEAR_OFF_TIME)

    inst:SetStateGraph("SGmagicalgirl_mami")
    inst:SetBrain(brain)

    return inst
end

return Prefab("magicalgirl_mami", fn, assets)
